#include <QStringList>
#include "errorlog.h"

QList<ErrorLog::ErrorEntry>* ErrorLog::s_entries = new QList<ErrorLog::ErrorEntry>();
int ErrorLog::s_errorCount = 0;
int ErrorLog::s_warningCount = 0;

void ErrorLog::error( const YYLTYPE& loc, const QString& message )
{
    ErrorEntry e;
    e.kind = Error;
    e.location = loc;
    e.message = message;
    s_entries->append( e );
    s_errorCount++;
}

void ErrorLog::warning( const YYLTYPE& loc, const QString& message )
{    
    ErrorEntry e;
    e.kind = Warning;
    e.location = loc;
    e.message = message;
    s_entries->append( e );
    s_warningCount++;
}

bool ErrorLog::hasErrors()
{
    return s_errorCount > 0;
}

bool ErrorLog::hasWarnings()
{
    return s_warningCount > 0;
}

bool ErrorLog::isEmpty()
{
    return s_errorCount == 0 && s_warningCount == 0;
}

QString ErrorLog::output()
{
    QStringList lst;

    foreach( ErrorEntry e, *s_entries )
    {
        QString txt;
        switch( e.kind )
        {
        case Error:
            txt = "Error: ";
            break;
        case Warning:
            txt = "Warning: ";
            break;
        }

        txt += QString("%1:%2 %3").arg(e.location.first_line).arg(e.location.first_column).arg(e.message);
        lst.append(txt);
    }

    return lst.join("\n");
}

int ErrorLog::errorCount()
{
    return s_errorCount;
}

int ErrorLog::warningCount()
{
    return s_warningCount;
}


